// pages/loupan/detail.js
var app =getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id:'',
		type:'',
		detail:'',
		moretext:"点击查看更多",
		showmore:false,
		liked:false,
		imglist:[],
    wuyetype:'', // 物业类型
		wenda:[],
    isDesc:'',//楼盘详情
    isDisconuntInfo:'',//优惠信息
    IsDiscountinfo:false, //是否展示优惠信息
    isShowkefu:false, //客服
    currentId: '',
    StartTime: '',
    EndTime: '',
		modalName: '',
		picker: [
			'男', '女'
		],
		value: '男',
		name: '',
		tel: '',
		huxingimg:'',
		xiangqingmore:'点击查看更多',
		xinagqingshow:false,
    opid:''
	},

	/**
	 * 生命周期函数--监听页面加载 
	 */

	morexiangqing(){
		var show = this.data.xinagqingshow;

		if (show == true) {
			console.log(show)
			this.setData({
				xinagqingshow: false,
				xiangqingmore: '点击查看更多'
			})
		}
		else if (show == false) {
			console.log(show)
			this.setData({
				xinagqingshow: true,
				xiangqingmore: '点击收起'
			})
		}
	},
	onLoad: function (options) {
 			console.log(options);
    var opid = wx.getStorageSync('user').openid;
			var that =this;
			that.setData({
				id:options.id,
				type:options.type,
        opid: opid
			})
			that.detail();
	},
	tel(e){
		var tel = e.currentTarget.dataset.tel;
		wx.makePhoneCall({
			phoneNumber: tel // 仅为示例，并非真实的电话号码
		})
	},
		detail(){
			wx.showLoading({
				title: '加载中', 
			})
			var id = this.data.id;
			var that =this;
      var opid = wx.getStorageSync('user').openid;
      app.core('get', 'get_houses_detail/id/' + id +'/open_id/'+opid+'',{}
			,(res)=>{
				console.log(res)
        //有优惠信息的时候展示，没有就不展示
				wx.hideLoading()

        if (res.data.discountInfo){
          that.setData({
            IsDiscountinfo:true,
            isShowkefu:true,
            isDisconuntInfo: res.data.discountInfo
          });
        }else{
          that.setData({
            IsDiscountinfo: false,
            isShowkefu:false
          });
        }

        that.setData({
          isDesc: res.data.newhouse_project_extend.projectInfo
        });
        var newres =res.data;
        var wuyetype = res.data.propertyType;
        switch(wuyetype){
          case "-1-3-":
            wuyetype ="-住宅-写字楼-"
            break;
          case "-1-2-":
            wuyetype = "-住宅-商铺-"
            break;
          case "-1-2-3":
            wuyetype = "-住宅-商铺-写字楼-"
            break;
          case "-2-3-":
            wuyetype = "-商铺-写字楼-"
            break;
					case "-1-":
						wuyetype = "-住宅-"
						break;
        }
        
        var renovateState = res.data.renovateState;
        var ressss = ''
        switch (renovateState) {
          case 1:
            ressss = "毛坯"
            break;
          case 2:
            ressss = "简装"
            break;
          case 3:
            ressss = "精装"
            break;
      
        }
      console.log(ressss,11222)

				// var arr =[];
				// if (res.data.buildNoPhoto){
				// 	arr.push(res.data.buildNoPhoto);
				// }
				// if (res.data.effectPhoto) {
				// 	arr.push(res.data.effectPhoto);
				// }
				// if (res.data.trafficPhoto) {
				// 	arr.push(res.data.trafficPhoto);
				// }
        // if (res.data.dimensionPhoto) {
        //   arr.push(res.data.dimensionPhoto);
        // }
        // if (res.data.videoPhoto) {
        //   arr.push(res.data.videoPhoto);
        // }
        that.setData({
          wuyetype: wuyetype,
          detail: res.data,
          liked: res.hasCollect,
          renovateState:ressss
        })

        var imglists = [];
				var huxingimg = [];
        for (var i = 0; i < res.data.newhouse_house_type.length;i++){
					huxingimg.push(res.data.newhouse_house_type[i].photoPath)
        }
				imglists.push(res.data.buildNoPhoto);
				imglists.push(res.data.effectPhoto);
				imglists.push(res.data.sandPhoto);
				that.setData({
          imglist: imglists,
					huxingimg: huxingimg
				})
				var x = res.data.loupan_wenda;
				var wenda=[];
				for(var i =0;i<x.length;i++){
					if (x[i].p_wenda_id == 0){
						wenda.push(x[i])
					}
				}
				that.setData({
					wenda:wenda
				})
				//console.log(arr)
			}
			)
		},// 获取详情
	btnliked(){

		var that = this;
		var liked = that.data.liked;
		if(liked == false){
			that.liked()
		}else {
			that.dislike()
		}
	},
	liked(){
		var opid = wx.getStorageSync('user').openid;
		var id = this.data.id;
		var liked = this.data.liked;

			app.core('get', 'create_user_collect/open_id/' +opid+'/type/1/lid/' + id + '', '', (res => {
				this.setData({
					liked: true,
				})
				console.log(this.data.liked)
				wx.showToast({
					title: '收藏成功',
					icon: 'none',
					duration: 1000
				})
			}))
		

	},// 收藏
	dislike() {

		var that = this;
		var id = that.data.id;
		var like = that.data.liked;
		var opid = wx.getStorageSync('user').openid;

		app.core('get', 'cancel_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
			console.log(res)
			wx.showToast({
				title: '取消收藏',
				icon: 'none',
				duration: 1000
			})
			that.setData({
				liked: false
			})
		}))


	},// 取消收藏

	previewImage(e){
		var current = e.target.dataset.src;
    console.log(current);
		wx.previewImage({
			current: current, // 当前显示图片的http链接  
	 		urls:this.data.huxingimg
		})

	},//预览图片
	previewImagess(e) {
		var current = e.target.dataset.src;
		console.log(current);
		wx.previewImage({
			current: current, // 当前显示图片的http链接  
			urls: this.data.imglist
		})

	},//预览图片
	addmore(){
		var show = this.data.showmore;
		
		if(show == true){
			console.log(show)
			this.setData({
				showmore: false,
				moretext:'点击查看更多'
			})
		}
		else if(show == false){
			console.log(show)
			this.setData({
				showmore: true,
				moretext: '点击收起'
			})
		}
		
	},
	// 点击加载更多
	share(){
		wx.updateShareMenu({
			withShareTicket: true,
			success(res) {
				console.log(res)
			 }
		})
	},
	// 分享转发
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
    var timestamp = Date.parse(new Date()); //进入页面的时间戳
    var Stimestamp = timestamp / 1000;
    this.setData({
      StartTime: Stimestamp
    });
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
    var timestamp = Date.parse(new Date()); //离开页面的的时间戳
    var Etimestamp = timestamp / 1000;
    this.setData({
      EndTime: Etimestamp
    });
    app.core('post', 'create_data_statistics', {
      // data 部分
      'type': 1,
      'visit_time': this.data.EndTime - this.data.StartTime,
      'read_id': this.data.id,
      'open_id': wx.getStorageSync('user').openid
    }, (res) => {
      console.log(res)
    },
      (err) => {
        console.log(err)
      }
    )
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {
		return {
			title: this.data.detail.projectName,
			path: 'pages/loupan/detail?id='+this.data.id+''
		}
	},


  //客服
  gocontent: function(){
   wx.makePhoneCall({
		 phoneNumber: '400-887-1216'
   })
 },

  sqyh(){
    this.showModal()
  } , // 申请优惠

	showModal(e) {


		this.setData({
			modalName: e.currentTarget.dataset.target,
		
		})


	},
	pickerchange(e) {
		console.log(e);
		var name = this.data.picker[e.detail.value]
		this.setData({
			value: name
		})
	},

	getname(e) {
		this.setData({
			name: e.detail.value
		})

	},
	gettel(e) {
		this.setData({
			tel: e.detail.value
		})

	},

	hideModal(e) {

		this.setData({
			modalName: null
		})
	},
	submitmodal(){
		var project_id = this.data.houseid;
		var that = this;
		var name = that.data.name;
		var phone = that.data.tel;
		var sex = that.data.value;
	if(name == ''){
		wx.showToast({
			title: '请输入姓名',
			duration:1000,
			icon:'none'
		});
		return;
	}else if(phone == ''){
		wx.showToast({
			title: '请输入手机号码',
			duration:1000,
			icon:'none'
		})
	}else{
		app.core('post', 'order_project ', {
			name: name,
			phone: phone,
			type: 1,
			project_id: project_id,
			sex: sex
		}, (res => {
				this.setData({
					modalName: null
				})
			wx.showToast({
				title: '已申请优惠',
				duration: 1000,
				icon: 'none'
			})

		})) 												

	}



	}
})